<template>
  <div>
    <vab-card>
      <span class="leve_title">账户管理</span>
    </vab-card>
    <vab-card shadow="always">
      <template #header>
        <div class="flex_sb">
          <span class="leve2_title">账户列表</span>
          <div class="flex_just_end">
            <VabAuthButton
              type="primary"
              title="添加"
              @click="createRoleRef.open('create', undefined)"
            />
          </div>
        </div>
      </template>
      <el-table
        border
        height="580"
        style="width: 100%"
        :data="dataSource"
        v-loading="loading"
      >
        <el-table-column label="序号" align="center" width="60">
          <template #default="scope">
            <span
              class="ellipsis"
              v-text="(data.pageNum - 1) * data.pageSize + (scope.$index + 1)"
            ></span>
          </template>
        </el-table-column>
        <el-table-column
          v-for="(item, i) in data.cloums"
          :key="i"
          :prop="item.prop"
          :label="item.label"
          :align="'center'"
        >
          <template #header>
            <span class="text_center">{{ item.label }}</span>
          </template>
          <template #default="{ row }">
            <!-- 自定义 -->
            <div v-if="item.type == 'custom'">
              <span v-if="item.prop == 'is_master' || item.prop == 'status'">
                <el-tag :type="statusColor(row[item.prop])">
                  {{ row[item.prop] ? '是' : '否' }}
                </el-tag>
              </span>
            </div>
            <!-- 正常 -->
            <div v-else class="ellipsis">
              {{ _FieldFormat(row[item.prop], item.type) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200" fixed="right">
          <template #default="{ row }">
            <VabAuthButton
              title="编辑"
              update
              v-if="!row.is_master"
              @click="createRoleRef.open('update', row)"
            />
            <VabAuthButton title="重置密码" @click="resetPwdRef.open(row)" />
          </template>
        </el-table-column>
      </el-table>
    </vab-card>
    <!-- 添加/编辑权限 -->
    <CreateRole ref="createRoleRef" @refreshPage="_GetProductList" />
    <!-- 重置密码 -->
    <ResetPwd ref="resetPwdRef" @refreshPage="_GetProductList" />
  </div>
</template>
<!-- 库存查询 -->
<script lang="ts" setup>
  defineOptions({
    name: 'SetRole',
  })
  /**
   * @import
   */
  import { BaseUserInfo, DateFormat, FieldFormat } from '~/src/utils/utils'
  import { roleCloums } from './components/tools'
  import CreateRole from './components/CreateRole.vue'
  import ResetPwd from './components/ResetPwd.vue'
  import { customerAccounts, customerCategory } from '~/src/api/dms/customer'
  /**
   * @var
   */
  const proxy: any = getCurrentInstance()?.proxy
  const router = useRouter()
  const createRoleRef = ref()
  const resetPwdRef = ref()

  const loading = ref(false)
  const dataSource = ref<any>([])

  const data = reactive<any>({
    dataSource: [],
    cloums: [],
    pageNum: 1,
    pageSize: 20,
    count: 0,
  })
  data.dataSource = []
  data.cloums = roleCloums

  /**
   * @method
   */

  // 点击事件
  const handleClick = (type: any, row: any = {}) => {}

  // 字段级格式化
  const _FieldFormat = (value: string, type = '') => {
    if (type == 'time') return DateFormat(value, 'YYYY-MM-DD')
    return FieldFormat(value, type)
  }

  // 状态tag_color
  const statusColor = (value: number) => {
    if (value == 0) return 'danger'
    if (value == 1) return 'success'
    return 'info'
  }

  // 获取数据
  const _GetProductList = async () => {
    loading.value = true
    dataSource.value = []

    const { uid } = BaseUserInfo()

    try {
      const { data } = await customerAccounts({ group_cust_id: uid })

      dataSource.value = data
      loading.value = false
    } catch (error) {}
  }
  onMounted(() => {
    _GetProductList()
  })
</script>

<style lang="scss" scoped>
  @import '../../components/public.scss';
</style>
